package com.yanqu.road.server.gameplayer.module.cricket.cmd;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.cricket.CricketData;
import com.yanqu.road.entity.cricket.CricketMatchData;
import com.yanqu.road.entity.cricket.UserCricketData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.cricket.CricketMgr;
import com.yanqu.road.server.gameplayer.module.cricket.CricketModule;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketBattle;
import com.yanqu.road.server.gameplayer.module.cricket.model.CricketBattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.CricketGameResult;
import com.yanqu.road.server.gameplayer.module.cricket.model.CricketMatchUserData;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;


@Cmd(code = GameProtocol.S_CRICKET_BATTLE_COMPARE, desc = "蛐蛐切磋")
public class CricketBattleCompareCmd extends AbstractCommand {

    private CricketProto.CricketBattleCompareReqMsg reqMsg;

    private CricketProto.CricketBattleCompareRespMsg.Builder respMsgBuilder;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        this.reqMsg = CricketProto.CricketBattleCompareReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        this.respMsgBuilder = CricketProto.CricketBattleCompareRespMsg.newBuilder();
        this.respMsgBuilder.setRet(0);
        CricketModule cricketModule = player.getModule(CricketModule.class);
        CricketData cricketData = cricketModule.getCricketData(this.reqMsg.getCricketId());
        if (cricketData == null) {
            this.respMsgBuilder.setRet(GameErrorCode.E_CRICKET_CRICKET_NOT_FOUND);
            return;
        }

        CricketBattleData cricketBattleData = cricketModule.toCricketBattleData(this.reqMsg.getCricketId());
        CricketBattleData cricketBattleData1 = CricketMgr.toCricketBattleData(this.reqMsg.getTargetCricket());
        CricketBattle cricketBattle = new CricketBattle();
        CricketGameResult cricketGameResult = cricketBattle.gameRound(cricketBattleData, cricketBattleData1);
        this.respMsgBuilder.setGameRound(cricketGameResult.getCricketGameRoundBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {
        player.sendPacket(ClientProtocol.U_CRICKET_BATTLE_COMPARE, respMsgBuilder);
    }
}
